import itertools

TARGET = 24
EPSILON = 1e-6
ADD, MULTIPLY, SUBTRACT, DIVIDE = 0, 1, 2, 3


def generate_expr(nums, current_exprs=None):
    if current_exprs is None:
        current_exprs = [(str(num), num) for num in nums]

    if len(current_exprs) == 1:
        _, value = current_exprs[0]
        if abs(value - TARGET) < EPSILON:
            return [current_exprs[0][0]]
        return []

    results = []
    size = len(current_exprs)
    for i in range(size):
        for j in range(size):
            if i != j:
                next_exprs = [current_exprs[k] for k in range(size) if k != i and k != j]
                for op in range(4):
                    if op < 2 and i > j:
                        continue

                    expr1, val1 = current_exprs[i]
                    expr2, val2 = current_exprs[j]
                    new_expr = ""

                    if op == ADD:
                        new_expr = f"({expr1}+{expr2})"
                        next_exprs.append((new_expr, val1 + val2))
                    elif op == MULTIPLY:
                        new_expr = f"({expr1}*{expr2})"
                        next_exprs.append((new_expr, val1 * val2))
                    elif op == SUBTRACT:
                        new_expr = f"({expr1}-{expr2})"
                        next_exprs.append((new_expr, val1 - val2))
                    elif op == DIVIDE:
                        if abs(val2) < EPSILON:
                            continue
                        new_expr = f"({expr1}/{expr2})"
                        next_exprs.append((new_expr, val1 / val2))

                    sub_results = generate_expr([], next_exprs)
                    for result in sub_results:
                        results.append(result)
                    next_exprs.pop()

    return results

def find_prime_factor_product(nums):

    #1-10分别映射如下字段
    prime_map = {index+1: prime for index, prime in enumerate([2, 3, 5, 7, 9, 11, 13, 17, 19, 23])}
    result = 1

    for num in nums:
          result = result * prime_map[num]

    return result
def eval_expr(expr):
    try:
        return abs(eval(expr) - 24) < 1e-6
    except ZeroDivisionError:
        return False


def find_expressions_for_24(nums):
    results = set()

    for p_nums in set(itertools.permutations(nums)):
        for expr in generate_expr(list(p_nums)):
            # 符合24点
            if eval_expr(expr):
                results.add(expr)
    return results


# Generate all combinations of 4 numbers from 1 to 10, without considering permutations
all_combinations = set(itertools.combinations_with_replacement(range(1, 11), 4))

# Find all expressions that result in 24 for each combination
expressions_resulting_in_24 = {}
for combo in all_combinations:
    prime_factors_product = find_prime_factor_product(combo)
    expressions = find_expressions_for_24(combo)
    if expressions:
        expressions_resulting_in_24[prime_factors_product] = expressions

# Display the results
for combo, exprs in list(expressions_resulting_in_24.items()):
    print(f"Combination {combo}:")
    for expr in exprs:
        print(f"  {expr}")
    print()
